import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.14
import QtQuick.Controls.Material 2.12

import "qrc:/_context/Basic"
import "qrc:/_context/CusCheckBox"
import "qrc:/_context/CusInput"
import "qrc:/_context/CusGroupBox"
import "qrc:/_context/CusComboBox"
import "qrc:/_context/CusButton"
import ModbusHandler 1.0

Item{
    id: modbus_4;

    property string modbusClientNum: "4";

    property var modbusJsonData: {
        "modbusInfo": []
    }

    property int funcCode: 3;
    // 存放多个组件的数组
    property var objList: new Array;

    property alias text: enableLbl.text;
    property int fontsize: 19;

    ModbusHandler{
        id: modbusHandler;
    }

    ColumnLayout {
        // anchors.fill: parent;
        Layout.fillWidth: true;
        Layout.fillHeight: true;
        spacing: 10;

        RowLayout{
            spacing: 20;

            YaheiText{
                id: enableLbl;
                color: "#FFFF00";
                font.bold: true;
            }

            Switch{
                font.pixelSize: fontsize - 2
                indicator.height: 18
                font.family: "Microsoft Yahei"
                text: "是否启用"
                Material.accent: "#EC3315"
            }
        }

        RowLayout{
            width: parent.width;
            spacing: 10;

            YaheiText{
                text: "Modbus通讯类型：";
            }
            RadioButton{
                id: modbus_tcp;
                text: "TCP";

                checked: true;
            }
            RadioButton{
                id: modbus_rtu;
                text: "RTU";
            }

            // 分割线
            // Rectangle {
            //     height: 24;
            //     width: 2;
            //     color: tingeOpacityColor;
            // }
        }


        RowLayout{
            YaheiText{
                text: "从站ID：";
            }

            HldTextField{
                id: slave_id;
                text: "1";
            }

            // 分割线
            // Rectangle {
            //     height: 24;
            //     width: 2;
            //     color: tingeOpacityColor;
            // }
        }
        RowLayout{
            YaheiText{
                text: "功能码：";
            }
            RadioButton{
                text: "01(读取线圈状态)";
                onClicked: {
                    if(checked)
                    {
                        funcCode = 1;
                    }
                }
            }
            RadioButton{
                text: "02(读取输入状态)";
                onClicked: {
                    if(checked)
                    {
                        funcCode = 2;
                    }
                }
            }
            RadioButton{
                text: "03(读取保持寄存器)";
                checked: true;
                onClicked: {
                    if(checked)
                    {
                        funcCode = 3;
                    }
                }
            }
            RadioButton{
                text: "04(读取输入寄存器)";
                onClicked: {
                    if(checked)
                    {
                        funcCode = 4;
                    }
                }
            }
        }

        CusGroupBox{
            title: "寄存器设置：";
            Layout.fillWidth: true;

            Column{
                id: column;
                spacing: 20;

                RowLayout{
                    spacing: 12;

                    YaheiText{
                        text: "起始地址:";
                    }
                    HldTextField{
                        id: addrBegin;
                        width: 80;
                        text: "40001";
                    }

                    YaheiText{
                        text: "结束地址:";
                    }
                    HldTextField{
                        id: addrEnd;
                        width: 80;
                        text: "40101";
                    }

                    // 步长
                    // YaheiText{
                    //     text: "步长:";
                    // }
                    // CusComboBox{
                    //     model:["1", "2"];
                    // }

                    // 工况点类型
                    YaheiText{
                        text: "工况点类型:";
                    }
                    // 数字量、信号量、开关量、状态量、报警量
                    CusComboBox{
                        id: _opType;

                        implicitWidth: 180;
                        // "双浮点数(abcd)", "双浮点数(badc)"
                        model:["数字量#", "开关量", "模拟量(无符号)*", "单浮点数(abcd)*", "单浮点数(badc)", "单浮点数(dcba)", "模拟量(有符号)"];
                    }

                    // 工况点的系数：
                    YaheiText{
                        text: "工况点系数:";
                    }
                    // 数据缩小10倍
                    CusComboBox{
                        // id: _opCoefficient_Shrink;
                        id: _opCoefficient;
                        model:["÷/x 1", "÷ 10", "÷ 100", "x 10", "x 100"];
                    }

                    CusButton{
                        text: "新增";

                        onClicked: {
                            var modbusItem = createObject();
                            objList.push(modbusItem);
                        }
                    }
                }
            }

        }

        CusButton{
            width: 160;
            height: 46;
            text: "生成配置";

            onClicked: {
                for(var i = 0; i < objList.length; i++){
                    if(typeof(objList[i]) === "undefined")
                    {
                        continue;
                    }
                    else{
                        console.log(objList[i].g_registerBegin);
                        console.log(objList[i].g_OperatePointType);

                        var _modbusData = {
                            "addrBegin": objList[i].g_registerBegin,
                            "addrEnd": objList[i].g_registerEnd,
                            "opePointType": objList[i].g_OperatePointType,
                            "opePointCoeffi": objList[i].g_OperatePointCoeffi
                        };

                        modbusJsonData.modbusInfo.push(_modbusData);
                    }
                }

                generateJsonData();
            }
        }
    }

    function componentDestroy(object) {
        var index = objList.indexOf(object);
        console.log("Delete " + index);

        delete objList[index];

        object.destroy();
    }

    function createObject() {
        // createComponent from external file "MyItem.qml"
        var component = Qt.createComponent("ModbusItem.qml")
        if (component.status === Component.Ready) {
            var obj = component.createObject(column, { "color": "#252528",
                                                 "width": column.width,
                                                 "height": 50,
                                                 "textInformation": 'createComponent from external file "ModbusItem.qml"' })
            obj.destroyMyself.connect(componentDestroy)
        }

        // createComponent from internal file myComponent
        // var obj2 = myComponent.createObject(column, { "color": "red",
        //                                         "width": column.width,
        //                                         "height": 50,
        //                                         "textInformation": "createComponent from internal file myComponent" })
        // obj2.destroyMyself.connect(componentDestroy)

        return obj;
    }

    function createQmlObject(){
        var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "blue"; width: 1024; height: 50}',
                                           column,
                                           "");
    }

    // 生成JSON数据并传递给JsonHandler对象
    function generateJsonData() {

        // console.log("Test");

        var fileName = "Modbus";
        var protocolType;

        if(modbus_tcp.checked)
        {
            protocolType = modbus_tcp.text;
        }
        else if(modbus_rtu.checked)
        {
            protocolType = modbus_rtu.text;
        }

        fileName += protocolType;

        var jsonData = {
            "cliNum": modbusClientNum,
            "fileName": fileName,
            "protocolType": protocolType,
            "funCode": funcCode,
            "slaveId": parseInt(slave_id.text),

            "modbusInfo": [
                {
                    "addrBegin": addrBegin.text,
                    "addrEnd": addrEnd.text,
                    "opePointType": _opType.currentIndex,
                    "opePointCoeffi": _opCoefficient.currentIndex
                }
            ]
        };

        for(var i = 0; i < modbusJsonData.modbusInfo.length; i++)
        {
            jsonData.modbusInfo.push(modbusJsonData.modbusInfo[i]);
        }

        modbusHandler.handleJsonData(JSON.stringify(jsonData));
    }
}
